package com.example.supernote.ui.note.components

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.example.supernote.R
import com.example.supernote.model.note.context.Context
import com.example.supernote.ui.components.UniformDialog
import com.example.supernote.ui.components.UniformPreviewBody
import com.example.supernote.ui.components.UniformToast

@Composable
fun ShowTableDialog(
    onCancel: () -> Unit,
    onConfirm: (String) -> Unit
) {
    var rows by remember { mutableStateOf("3") }
    var cols by remember { mutableStateOf("3") }
    val toastContext = LocalContext.current
    val warnInfo = stringResource(R.string.note_table_warn)
    val modifyRows: (String) -> Int = {
        try {
            it.toInt()
        } catch (e: Exception) {
            UniformToast(toastContext, warnInfo)
            rows = "3"
            3
        }
    }
    val modifyCols: (String) -> Int = {
        try {
            it.toInt()
        } catch (e: Exception) {
            UniformToast(toastContext, warnInfo)
            cols = "3"
            3
        }
    }
    val markDownTable = getMarkdownTableString(
        modifyRows(rows),
        modifyCols(cols),
        stringResource(R.string.note_table_header),
        stringResource(R.string.note_table_item)
    )
    UniformDialog(
        title = stringResource(R.string.note_table),
        onCancel = onCancel,
        onConfirm = { onConfirm(markDownTable) },
        body = {
            UniformPreviewBody(
                title = stringResource(R.string.note_table_preview),
                html = Context(article = TextFieldValue(markDownTable)).parseContext()
            )
            TextField(
                value = rows,
                modifier = Modifier
                    .padding(end = 5.dp)
                    .fillMaxWidth(),
                onValueChange = { rows = it },
                singleLine = true,
                label = {
                    Text(
                        text = stringResource(R.string.note_table_rows),
                        color = Color.Gray,
                        overflow = TextOverflow.Visible
                    )
                }
            )
            TextField(
                value = cols,
                modifier = Modifier
                    .padding(end = 5.dp)
                    .fillMaxWidth(),
                onValueChange = { cols = it },
                singleLine = true,
                label = {
                    Text(
                        text = stringResource(R.string.note_table_cols),
                        color = Color.Gray,
                        overflow = TextOverflow.Visible
                    )
                }
            )
        }
    )
}

fun getMarkdownTableString(rows: Int, cols: Int, headerText: String, itemText: String): String {
    // 创建表头
    val header = (1..cols).joinToString(separator = "|", prefix = "| ", postfix = " |") {
        "$headerText$it"
    }

    // 创建分隔线
    val separator = (1..cols).joinToString(separator = "|", prefix = "|", postfix = "|") { "---" }

    // 创建表格内容
    val builder = StringBuilder()
    builder.append(header).append("\n\n")
    builder.append(separator).append("\n\n")

    for (i in 1..rows) {
        val r = (1..cols).joinToString(separator = "|", prefix = "| ", postfix = " |") { itemText }
        builder.append(r).append("\n\n")
    }

    return builder.toString()
}